package cn.gailvlun.gll.presentation.base;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.webkit.DownloadListener;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.ActivityWebViewBinding;
import cn.gailvlun.gll.util.LogUtil;
import cn.gailvlun.gll.widget.NavigationBarView;



public class WebViewActivity extends BaseActivity {

    public static void openActivity(Context context, String title, String url) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("url", url);
        context.startActivity(intent);
    }

    public static void openUserProtocolActivity(Context context) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra("title", "用户协议");
        intent.putExtra("url", "https://dev.xiaogaijun.com/doc/useragreement/");
        context.startActivity(intent);
    }

    public static void openChargeProtocolActivity(Context context) {
        openActivity(context, "充值及余额常见问题", "https://dev.xiaogaijun.com/static/webInApp/qa.html");
    }

    private ActivityWebViewBinding mBinding;

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_web_view);
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        String url = intent.getStringExtra("url");

        mBinding.nbv.setTitleText(title);
        mBinding.webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                LogUtil.d("shouldOverridUrlLoading: " + url);
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                LogUtil.d("error" + error.toString());
                handler.proceed();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                LogUtil.d("finished");
            }
        });
        mBinding.webView.getSettings().setJavaScriptEnabled(true);
        mBinding.webView.getSettings().setDomStorageEnabled(true);

        mBinding.webView.loadUrl(url);
    }
}
